//
//  YLAdViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/12/27.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLAdViewModel.h"
#import "WHNetworkTool.h"
#import "YLAccountTool.h"
#import "YLAdModel.h"
@implementation YLAdViewModel

- (RACCommand *)requestCommand
{
    if (!_requestCommand) {
        __weak typeof(self) weakSelf = self;
        _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            
            
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
               
                NSString *url = [NSString stringWithFormat:@"%@AppCommon/PGetUdefindNitoce",YLURL];
                
                [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:nil success:^(id responseObject) {
                    
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                        YLAdItem *item = [YLAdItem yy_modelWithDictionary:responseObject[@"httpResult"]];
                        if (item.item.count > 0) {
                            
                            YLAdModel *model = item.item.firstObject;
                            // 如果页面对了 继续
                           if (model.conditionId == weakSelf.conditionId) {
                                // 判断通知id 判断是不是新通知
                                YLAccount *account = [YLAccountTool account];
                                if (![account.noticeId isEqualToString:model.noticeId]) {
                                    // 是， 发送通知
                                    [subscriber sendNext:model];
                                    account.noticeId = model.noticeId;
                                    [YLAccountTool saveAccount:account];
                                }
                                
                            }
                            
                            
                        }

                    }
                    [subscriber sendCompleted];
                    
                    
                } fail:^(NSError *error) {
                    [subscriber sendCompleted];
                }];
                
                return nil;
                
                
            }];
            
            
        }];
    }
    return _requestCommand;
}

@end
